package com.cskaoyan.shopping.service.impl;

import com.cskaoyan.mall.constant.ShoppingRetCode;
import com.cskaoyan.shopping.constants.GlobalConstants;
import com.cskaoyan.shopping.converter.ContentConverter;
import com.cskaoyan.shopping.dal.entitys.PanelContent;
import com.cskaoyan.shopping.dal.persistence.PanelContentMapper;
import com.cskaoyan.shopping.dto.NavListResponse;
import com.cskaoyan.shopping.dto.PanelContentDto;
import com.cskaoyan.shopping.service.IContentService;
import com.cskaoyan.shopping.utils.ExceptionProcessorUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * 创建日期: 2022/04/24 12:42
 *
 * @author ChengWenqi
 */

@Service
@Slf4j
public class IContentServiceImpl implements IContentService {

    @Autowired
    PanelContentMapper panelContentMapper;

    @Autowired
    ContentConverter contentConverter;

    @Override
    public NavListResponse queryNavList() {
        NavListResponse response = new NavListResponse();

        try {
            // 查找 panel_content 表中所有 panel_id 为0的记录
            Example example = new Example(PanelContent.class);
            example.setOrderByClause(GlobalConstants.PANEL_CONTENT_ORDER);
            example.createCriteria().andEqualTo("panelId", GlobalConstants.HEADER_PANEL_ID);
            List<PanelContent> panelContents = panelContentMapper.selectByExample(example);

            // 转换成 dto
            List<PanelContentDto> panelContentDtos = contentConverter.panelContents2Dto(panelContents);
            response.setPannelContentDtos(panelContentDtos);
            // 设置成功响应码和响应信息
            response.setCode(ShoppingRetCode.SUCCESS.getCode());
            response.setMsg(ShoppingRetCode.SUCCESS.getMessage());

        } catch (Exception e) {
            log.error("IContentServiceImpl.queryNavList occur Exception: " + e);
            ExceptionProcessorUtils.wrapperHandlerException(response,e);
        }

        return response;
    }
}
